<template>
  <div>
    <div>【关键词条】</div>
    <div ref="wordRef" class="w-full h-full"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import * as echarts from 'echarts'
import 'echarts-wordcloud'

const props = defineProps({
  data: {
    type: Object,
    required: true
  }
})

// const customShape = "path://M300 50L400 200 300 350 100 350 0 200 100 50 300 50z"
const wordRef = ref(null)
let myChart: any = null
onMounted(() => {
  myChart = echarts.init(wordRef.value)
  renderChart()
})

const renderChart = () => {
  let option = {
    series: [
      {
        type: 'wordCloud',
        // shape: 'triangle',
        // drawOutOfBound: false,
        // left: 'center',
        // top: 'center',
        // width: '100%',
        // height: '100%',
        sizeRange: [8, 45],
        // 文字旋转范围
        rotationRange: [0, 0],
        // 单词之间的间距
        gridSize: 0,
        // 渲染动画
        layoutAnimation: true,
        textStyle: {
          color: randomRGB
        },
        // 高亮
        emphasis: {
          textStyle: {
            fontWeight: 'bold',
            color: '#FADADD'
          }
        },
        data: props.data.datas
      }
    ]

  }
  myChart.setOption(option)
}

// 生成随机颜色
const randomRGB = () => {
  const r = Math.floor(Math.random() * 255)
  const g = Math.floor(Math.random() * 255)
  const b = Math.floor(Math.random() * 255)
  return `rgb(${r}, ${g}, ${b})`
}

watch(() => props.data, () => {
  renderChart()
})

</script>

<style scoped></style>
